package com.apestech.framework.xml.plugin.node;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.plugin.callback.ExecNodeCallback;
import com.apestech.framework.xml.plugin.modul.SysCondition;
import com.apestech.framework.xml.pub.core.ElementUtil;
import org.dom4j.Element;

import java.util.List;

/**
 * @author hup
 * @create 2019-01-26-17:55
 */
public class NodeSysCondition extends ElementUtil {
    private Element node;
    private SysCondition sysCondition;

    public NodeSysCondition(SqlSession session, Element node){
        this.node=node;
        sysCondition=new SysCondition(session);
    }

    public void getCondition() throws Exception {
        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                Element eml = (Element) list.get(i);
                JSONObject attr=getNodeAttr(eml).getJSONObject("attrNode");
                if("model".equals(eml.getName())) sysCondition.setModel(attr);
                else if("tableName".equals(eml.getName())) sysCondition.setTableName(attr);
                else getFieldList(eml);
            }
        }
    }

    private void getFieldList(Element e) throws Exception {
        String theme=e.getName();
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                execSubChild(eml, new ExecNodeCallback(){
                    @Override
                    public void getNode(Element ele, String id, JSONObject attr, String text) throws Exception {
                        String eme=null;
                        String dbeme=sysCondition.getSession().getTheme();
                        int pos=id.indexOf(".");
                        if(pos>=0) eme=id.substring(0, pos);
                        else eme=theme;
                        if("dimension".equals(theme)){
                            if(text!=null && !"".equals(text)) attr.put("sql", text);
                            if(dbeme.equals(eme)){
                                sysCondition.addDimension(id, attr);
                            }
                        }
                        else if("control".equals(theme)){
                            if(dbeme.equals(eme)){
                                sysCondition.addControl(id, attr);
                            }
                        }
                        else if("roleField".equals(theme)){
                            if(dbeme.equals(eme)){
                                sysCondition.addRolefield(id, attr);
                            }
                        }
                    }
                });
            }
        }
    }

    public SysCondition getSysCondition() {
        return sysCondition;
    }
}
